import axios from "axios";

import router from '../router/index'
import { createRouter, createWebHistory } from 'vue-router';
import { ElMessage } from 'element-plus'


// import { getCookie } from "./cookie";
// import Cookies from "js-cookie"
const service = axios.create({
    baseURL: "/api",
    timeout: 5000
})
service.interceptors.request.use(config => {
    // console.log(Cookies,"getcookie()");
    // const token = getCookie();
    // console.log(token);
    let token = ""
    if (window.localStorage.getItem("userinfo")) {
        let obj = JSON.parse(window.localStorage.getItem("userinfo"));
        token = obj.token
    }
    if (token) {
        config.headers.token = token;
    }
    return config;
},
    error => {
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    response => {
        return response.data
    },
    error => {
        console.log(router, "trouter")
        console.log(error, "error")
        if (error.response.data.code == 400||error.status == 400) {
            ElMessage({
                message: '参数错误',
                type: 'warning',
                plain: true,
              })
        } else if (error.response.status == 401) {
            ElMessage({
                message: 'token已失效',
                type: 'warning',
                plain: true,
              })
            router.push({
                path: "/"
            })

        }
        return Promise.reject(error)

    }
)
export default service